/**
 * 导出试剂
 */
import { download } from "./index";

export const expReagentJson = (list: any[], fileName: string) => {
  let tempList: any = [];
  if (list && list.length) {
    list.forEach((item) => {
      const { name, cnname, casno, type, formula, volatile, viscose, note, mw, density, percentage, mp, bp, abbreviation } =
        item;
      tempList.push({
        Name: name,
        Abbreviation: abbreviation,
        "Chinese name": cnname,
        Casno: casno,
        "Molecular weight": mw,
        Density: density,
        Percentage: percentage,
        "Melting point": mp,
        "Boiling point": bp,
        Type: type,
        Formula: formula,
        Volatile: !!volatile ? true : false,
        Viscose: !!viscose ? true : false,
        note,
      });
    });
  }

  download(fileName, JSON.stringify(tempList));
};

export const expReagentExcel = (list: any[], fileName: string) => {
  //定义表头
  let str = `Name,Abbreviation,Chinese name,Casno,Molecular weight,Density,Percentage,Melting point,Boiling point,Type,Formula, Volatile,Viscose,Note\n`;

  let tempList: any[] = [];
  if (list && list.length) {
    list.forEach((item) => {
      const { name, cnname, casno, type, formula, volatile, viscose, note, mw, density, mp, bp, percentage, abbreviation } =
        item;
      tempList.push({
        Name: name,
        Abbreviation: abbreviation,
        "Chinese name": cnname,
        Casno: casno,
        "Molecular weight": mw,
        Density: density,
        Percentage: percentage,
        "Melting point": mp,
        "Boiling point": bp,
        Type: type,
        Formula: formula,
        Volatile: !!volatile ? "true" : "false",
        Viscose: !!viscose ? "true" : "false",
        note,
      });
    });
  }

  //增加\t为了不让表格显示科学计数法或者其他格式
  for (let i = 0; i < tempList.length; i++) {
    for (let item in tempList[i]) {
      str += `${(tempList[i][item] || "") + "\t"},`;
    }
    str += "\n";
  }
  //encodeURIComponent解决中文乱码
  let uri = "data:text/csv;charset=utf-8,\ufeff" + encodeURIComponent(str);
  //通过创建a标签实现
  let link = document.createElement("a");
  link.href = uri;
  //对下载的文件命名
  link.download = fileName;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
};

export const MockReagentData = [
  {
    Note: "distilled water",
    casno: "7732-18-5",
    cnname: "水",
    abbreviation: "",
    formula: "H2O",
    name: "Water",
    type: "pure_liquid",
    density: 1,
    mw: 18,
    bp: 100,
    mp: 0,
    percentage: 1,

  },
];

export const MockCasReagentData = [
  {
    casno: "7732-18-5",
  },
];

// 格式化导入的试剂json txt数据
export const formatJsonFile = (data: any[]) => {
  let list: any[] = [];
  if (data && data.length) {
    data.forEach((item: any) => {
      let obj: any = {};
      for (let key in item) {
        switch (key) {
          case "Casno":
            obj.casno = item[key];
            break;
          case "Chinese name":
            obj.cnname = item[key];
            break;
          case "Formula":
            obj.formula = item[key];
            break;
          case "Name":
            obj.name = item[key];
            break;
          case "note":
            obj.note = item[key];
            break;
          case "Type":
            obj.type = item[key];
            break;
          case "Viscose":
            obj.viscose = !!item[key] ? true : false;
            break;
          case "Volatile":
            obj.volatile = !!item[key] ? true : false;
            break;
          default:
            break;
        }
      }
      list.push(obj);
    });
  }
  return list;
};

// 格式化导入的试剂excel csv数据
export const formatXlsxFile = (data: any[]) => {
  let list: any[] = [];
  if (data && data.length) {
    data.forEach((item) => {
      let obj: any = {};
      for (let key in item) {
        switch (key) {
          case "Casno":
            let tmpCasno = item[key] + "";
            obj.casno = tmpCasno.replace(/\t$/g, "");
            break;
          case "Chinese name":
            obj.cnname = item[key].replace(/\t$/g, "");
            break;
          case "Formula":
            obj.formula = item[key].replace(/\t$/g, "");
            break;
          case "Name":
            obj.name = item[key].replace(/\t$/g, "");
            break;
          case "Note":
            obj.note = item[key].replace(/\t$/g, "");
            break;
          case "Type":
            obj.type = item[key].replace(/\t$/g, "");
            break;
          case "Viscose":
            obj.viscose =
              item[key].replace(/\t$/g, "") === "true" ? true : false;
            break;
          case "Volatile":
            obj.volatile =
              item[key].replace(/\t$/g, "") === "true" ? true : false;
            break;
          case " Volatile":
            obj.volatile =
              item[key].replace(/\t$/g, "") === "true" ? true : false;
            break;
          default:
            break;
        }
      }
      list.push(obj);
    });
  }
  return list;
};
